import 'package:Sayagh/Configs.dart';
import 'package:Sayagh/tablepress/csv.dart';
import 'package:flutter/material.dart';

class Goods extends StatefulWidget {
  final String _goodsUrl;
  final String _tableName;
  final String _catrgoryName;
  Goods(this._goodsUrl, this._tableName, this._catrgoryName);
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _Goods();
  }
}

class _Goods extends State<Goods> {
  @override
  Widget build(BuildContext context) {
    String essentialsUrl =
        "${widget._goodsUrl}/wp-content/tables/${widget._tableName}.csv";
    Future<List<List>> essentialsList =
        csv(widget._goodsUrl, widget._tableName, widget._catrgoryName);

    return Scaffold(
      appBar: AppBar(
        title: Text(
          widget._catrgoryName,
          textAlign: TextAlign.center,
        ),
        centerTitle: true,
      ),
      body: FutureBuilder(
        future: essentialsList,
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.data == null) {
            return loading;
          } else {
            List<String> head = snapshot.data[0];
            return ListView.builder(
                itemCount: snapshot.data[1].length - 1,
                itemBuilder: (BuildContext context, int index) {
                  List<String> prices = snapshot.data[1][index];

                  Color color = Colors.cyan;
                  if (index % 2 == 0) {
                    color = Colors.tealAccent;
                  }
                  String text = "";
                  for (int i = 0; i < head.length; i++) {
                    text = text + "\n" + head[i] + ":" + prices[i];
                  }

                  return Card(
                      color: color,
                      child: Text(
                        text,
                        textDirection: TextDirection.rtl,
                        textAlign: TextAlign.center,
                      ));
                });
          }
        },
      ),
    );
  }
}
